package main

import (
	"encoding/json"
	"fmt"
)

var (
	jsonStr []byte
)

func main() {
	nestedStructDemo()
	unmarshal()
}

type User struct {
	Name    string    `json:"name"`
	Email   string    `json:"email,omitempty"`
	Hobby   []string  `json:"hobby,omitempty"`
	Profile []Profile `json:"profile,omitempty"`
}

type Profile struct {
	Website string `json:"site"`
	Slogan  string `json:"slogan"`
}

func nestedStructDemo() {
	u1 := User{
		Name:  "七米",
		Hobby: []string{"足球", "双色球"},
		Profile: {
			Profile: {},
		},
	}
	b, err := json.Marshal(u1)
	if err != nil {
		fmt.Printf("json.Marshal failed, err:%v\n", err)
		return
	}
	fmt.Printf("str:%s\n", b)
	jsonStr = b
}

func unmarshal() {
	var req User
	if err := json.Unmarshal(jsonStr, &req); err != nil {
		fmt.Println("unmarshal is err:", err)
		return
	}
	fmt.Printf("unmarshal:%#v \n", req)
}
